package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ClassyTypeBean.ClassyType;

import java.util.ArrayList;

public class ClassyTypeTypeAdapter extends BaseAdapter {
    ArrayList<ClassyType> datas;
    Context context;
    LayoutInflater inflater;
    int selectedItem = -1;

    public ClassyTypeTypeAdapter(ArrayList<ClassyType> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position).getName();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.classy_type_type_item, null);
            }
            holder.name = (TextView) convertView.findViewById(R.id.name);
            if (position < datas.size()) {
                holder.name.setText(datas.get(position).getName());
                if (position == selectedItem) {
                    holder.name.setTextColor(Color.RED);
                } else {
                    holder.name.setTextColor(Color.BLACK);
                }
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return convertView;
    }

    /**
     * 设置当前被选中项
     *
     * @param position
     */
    public void setSelectedItem(int position) {
        this.selectedItem = position;
    }

    class Holder {
        TextView name;
    }
}
